package me.learningandroid.ch2_2.intentdemo;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.content.pm.ApplicationInfo;
import android.content.pm.ChangedPackages;
import android.content.pm.FeatureInfo;
import android.content.pm.InstrumentationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageInstaller;
import android.content.pm.PackageManager;
import android.content.pm.PermissionGroupInfo;
import android.content.pm.PermissionInfo;
import android.content.pm.ProviderInfo;
import android.content.pm.ResolveInfo;
import android.content.pm.ServiceInfo;
import android.content.pm.SharedLibraryInfo;
import android.content.pm.VersionedPackage;
import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.UserHandle;
import android.provider.MediaStore;
import android.widget.Button;
import android.widget.Toast;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    private static int TEXT_REQ_CODE=1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button explicitIntentBtn = (Button) findViewById(R.id.explicit_intent_btn);
        Button systemImplicitIntentBtn = (Button) findViewById(R.id.system_implicit_intent_btn);
        Button userImplicitIntentBtn = (Button) findViewById(R.id.user_implicit_intent_btn);
        Button intentWithExtraBtn = (Button) findViewById(R.id.intent_with_extra_btn);
        Button intentForResultBtn = (Button) findViewById(R.id.intent_for_result_btn);
        Button intentNotFoundBtn = (Button) findViewById(R.id.intent_not_found_btn);
        Button intentTestNotFoundBtn = (Button) findViewById(R.id.intent_with_package_manager_test_btn);

        // 显示intent用来跳转至同一应用的不同`Activity`
        explicitIntentBtn.setOnClickListener((view)-> {
            Intent intent = new Intent(this, me.learningandroid.ch2_2.intentdemo.ExplicitIntentActivity.class);
            startActivity(intent);
        });

        // 隐式intent用来跳转至系统提供的`Activity`
        systemImplicitIntentBtn.setOnClickListener((view)-> {
            Intent intent = new Intent(Intent.ACTION_DIAL);
            intent.setData(Uri.parse("tel:10086"));
            startActivity(intent);
        });

        // 自定义隐式Intent
        userImplicitIntentBtn.setOnClickListener((view)-> {
            Intent intent = new Intent();
            intent.setAction("me.learningandroid.ch2_2.intentdemo.APP2_START");
            // 每一个隐式intent都带有一个`Intent.CATEGORY_DEFAULT`的category，
            // 即默认地有`intent.addCategory(Intent.CATEGORY_DEFAULT);`
            // 所以需要隐式启动的activity，必须再`<intent-filter>`中申明`<category android:name="android.intent.category.DEFAULT" />`。
            startActivity(intent);
        });

        // Intent传递自定义数据
        intentWithExtraBtn.setOnClickListener((view)-> {
            Intent intent = new Intent(this, me.learningandroid.ch2_2.intentdemo.IntentWithExtraDataActivity.class);
            intent.putExtra("参数1",1);
            intent.putExtra("参数2","value");
            startActivity(intent);
        });

        // 返回数据
        intentForResultBtn.setOnClickListener((view)-> {
            Intent intent = new Intent(this, me.learningandroid.ch2_2.intentdemo.ExplicitIntentActivity.class);
            //
            startActivityForResult(intent, TEXT_REQ_CODE);
        });

        // 没有匹配活动的隐式Intent（应用崩溃）
        intentNotFoundBtn.setOnClickListener((view)-> {
            Intent intent = new Intent("me.learningandroid.ch2_2.intentdemo.NONEXISTENT");
            startActivity(intent);
        });

        // 测试隐式Intent是否有匹配活动
        intentTestNotFoundBtn.setOnClickListener((view)-> {
            Intent intent = new Intent("me.learningandroid.ch2_2.intentdemo.NONEXISTENT");
            // `PackageManager`成员方法`queryIntentActivities`提供通过intent查询匹配的活动列表。
            // 开发者应该检查`intent`是否有匹配的活动，避免程序崩溃，
            PackageManager packageManager = getPackageManager();
            List<ResolveInfo> matched = packageManager.queryIntentActivities(intent,PackageManager.MATCH_DEFAULT_ONLY);
            if (matched.size()==0) {
                Toast.makeText(this, "没有匹配的应用。", 5).show();
            } else {
                startActivity(intent);
            }

        });

    }

    //
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode,resultCode,data);
        if (resultCode== RESULT_OK) {
            if (requestCode == TEXT_REQ_CODE) {
                Toast.makeText(this,String.format("活动结果：%d, %s", TEXT_REQ_CODE, data.getExtras().toString()),3).show();
            }
        } else {
            Toast.makeText(this,"取消输入",3).show();
        }
    }
}